const http = require("http");
const fs = require("fs");
const path = require("path");
const server = http.createServer();
// 绑定
server.on("request", function (req, res) {
  const url = req.url;
  console.log(url);
  // 把请求到的url地址映射为本地文件的路径
  // const fpath = path.join(__dirname, url);
  var fpath = "";
  if (url === '/') {
    fpath = path.join(__dirname, './clock/index.html')
  } else {
    fpath=path.join(__dirname,'/clock',url)
  }
  // 根据映射的文件来读写文件
  fs.readFile(fpath, "utf8", function (err, dataStr) {
    if (err) {
      return res.end("404 not found");
    }
    res.end(dataStr);
  });
});
server.listen(80, function () {
  console.log("server listen at http://127.0.0.1");
});
